<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * 地区
 * @author 66diqiu-li
 *
 */
class Admin_area_model extends CI_Model
{
    private $table;
    
    static public $area = array();

    public function __construct()
    {
        parent::__construct();
        $this->table  = $this->db->dbprefix("area");
        $this->load->driver('cache');
    }

    public function get_area_by_id($id, $order = 'district_type ASC')
    {
        $retval = array();

        if($id == 0){
            return $this->get_continent();
        }

        $query = $this->db->query("SELECT * FROM {$this->table} WHERE id=? LIMIT 1", array($id));
        $code_row = $query->row_array();
        if(!empty($code_row))
        {
            $poi_code   = $code_row['poi_code'];
            $code_level = $code_row['code_level']+1;

            if($id == 40002){
                $code_level --;
                $poi_code = '0040002';
            }

            $query = $this->db->query("SELECT * FROM {$this->table} WHERE code_level=? AND poi_code LIKE ? ORDER BY $order", array(
                $code_level,
                $poi_code . '%',
            ));
            $retval = $query->result_array();
        }

        return $retval;
    }

    public function get_row($condition = array())
    {
        if ( ! empty($condition))
        {
            $query = $this->db->get_where($this->table, $condition);
            return $query->row_array();
        }
    }

    public function get_area_by_pinyin($pinyin)
    {
        $retval = array();

        $query = $this->db->query("SELECT * FROM {$this->table} WHERE pinyin=? LIMIT 1", array(
            $pinyin,
        ));
        if ($query->num_rows() > 0) {
            $retval = $query->result_array();
        }
        return $retval;
    }

    /**
     * @return array
     */
    public function get_continent()
    {
        $retval = array (
            0 =>
                array (
                    'sid' => '0',
                    'id' => '3900',
                    'name' => '亚洲',
                    'poi_code' => '0003900',
                    'code_level' => '1',
                    'district_type' => '1',
                ),
            1 =>
                array (
                    'sid' => '0',
                    'id' => '3600',
                    'name' => '欧洲',
                    'poi_code' => '0003600',
                    'code_level' => '1',
                    'district_type' => '1',
                ),
            2 =>
                array (
                    'sid' => '0',
                    'id' => '4100',
                    'name' => '大洋洲',
                    'poi_code' => '0004100',
                    'code_level' => '1',
                    'district_type' => '1',
                ),
            3 =>
                array (
                    'sid' => '0',
                    'id' => '3700',
                    'name' => '北美洲',
                    'poi_code' => '0003700',
                    'code_level' => '1',
                    'district_type' => '1',
                ),
            4 =>
                array (
                    'sid' => '0',
                    'id' => '4000',
                    'name' => '非洲',
                    'poi_code' => '0004000',
                    'code_level' => '1',
                    'district_type' => '1',
                ),
            5 =>
                array (
                    'sid' => '0',
                    'id' => '3800',
                    'name' => '南美洲',
                    'poi_code' => '0003800',
                    'code_level' => '1',
                    'district_type' => '1',
                ),
        );
        return $retval;
        /*$retval = array();
        $query = $this->db->query("SELECT * FROM {$this->table} WHERE code_level=1 AND district_type=1 ORDER BY id DESC");
        if ($query->num_rows() > 0) {
            $retval = $query->result_array();
        }
        return $retval;*/
    }

    public function get_area_name($aids = array(), $retstr = true)
    {
        if($aids == array(-1)){
            return null;
        }
        $retval = array();

        static $dists = array();
        $retstrkey = $retstr ? 'yes' : 'no';
        $key = md5(rtrim(implode(', ', $aids),', '));
        if(isset($dists[$retstrkey][$key])){
            return $dists[$retstrkey][$key];
        }

        foreach ($aids as $k => $id) {
            if($id == 0 || $id == -1){
                unset($aids[$k]);
                continue;
            }
//            if(isset(self::$area[$id])){
//                $retval[] = self::$area[$id];
//                unset($aids[$k]);
//            }
        }

        if($aids){
            $aid = rtrim(implode(', ', $aids),', ');

            $cache_key = 'a:'.md5($aid);

            if(!$retval = $this->cache->file->get($cache_key))
            {
                if(is_numeric($aid)){
                    $ind = " = $aid LIMIT 1";
                }else{
                    $ind = " IN ($aid)  ORDER BY code_level ASC, poi_code ASC";
                }
                $query = $this->db->query("SELECT * FROM {$this->table} WHERE id $ind ");

                if ($query->num_rows() > 0) {
                    $retval = $query->result_array();

                    $this->cache->file->save($cache_key, $retval, 2592000);
                }
            }
        }

//        if(DEBUG){
//            var_export($retval);
//        }
        if($retstr)
        {
            $retstr = '';
            foreach ($retval as $row)
            {
                $retstr .= '['.$row['name'].']';
            }
        }
        else
        {
            $retstr = array();
            foreach ($retval as $row)
            {
                $retstr[$row['id']] = $row['name'];
            }
        }

        $dists[$retstrkey][$key] = $retstr;

        return $retstr;
    }

    public function get_siblings($id, $with_parent = FALSE)
    {
        $query = $this->db->query("SELECT code_level,poi_code FROM {$this->table} WHERE id=$id");
        $retval = $query->row_array();
        if(empty($retval))
        {
            return array();
        }
        $poi_code = $retval['poi_code'];
        $code_level = $retval['code_level']-1;
        $poi_code = substr($poi_code, 0, -7);

        if($with_parent){
            $cmp = "code_level>=$code_level AND code_level<=".($code_level+1);
        }else{
            $cmp = "code_level>$code_level AND code_level<=".($code_level+1);
        }
        $sql = "SELECT * FROM {$this->table} WHERE $cmp AND poi_code LIKE '$poi_code%' ORDER BY code_level ASC, poi_code ASC";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
}